#
# OpenSistemas::Zabbix::Alarms
# Copyright (C) 2010 Open Sistemas <imasd@opensistemas.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
package OpenSistemas::Zabbix::Alarms;

use strict;
use warnings;

use Carp;
use OpenSistemas::OID::Item;
use NetSNMP::ASN (':all');
use DateTime;

use base 'OpenSistemas::OID::Tree';

my $alarmID       = '1.1.3.1.1.1';
my $alarmString   = '1.1.3.1.1.2';
my $alarmSeverity = '1.1.3.1.1.3';
my $alarmStatus   = '1.1.3.1.1.4';
my $alarmAck      = '1.1.3.1.1.5';
my $alarmLastUp   = '1.1.3.1.1.6';
my $hostID        = '1.1.3.1.1.7';

sub new
{
	my ($pkg, %params) = @_;

  croak 'Alarms: dhb parameter missing' if !exists ($params{dbh});
  croak 'Alarms: baseOID parameter missing' if !exists ($params{baseOID});

	my $self = {
    eventid => undef,
		%params
	};

	bless $self, $pkg;

	return $self;
}

sub cb_set
{
  my ($self, $oid, $value) = @_;
  my $eventid = $self->get($oid)->{eventid};
  my $dbh = $self->{dbh};

  $value = $value ? 1 : 0;
  $dbh->do("update events set acknowledged = $value where eventid = $eventid")
    or return 0;

  return 1;
}

sub update
{
  my $self = shift;
  my $dbh = $self->{dbh};

  my $sth = $dbh->prepare("select e.eventid as alarmID, t.description as alarmString, e.clock as alarmLastUp, e.acknowledged as alarmACK, t.priority as alarmSeverity, e.value as alarmStatus, h.host as hostID from events e, triggers t, hosts h, items i, functions f where e.objectid=t.triggerid and t.triggerid=f.triggerid and f.itemid=i.itemid and i.hostid=h.hostid and h.status=0 order by e.clock desc;");
  return 0 if !$sth;
  return 0 if !$sth->execute();
  while(my $ref = $sth->fetchrow_hashref())
  {
    $self->set(OpenSistemas::OID::Item->new({
      oid => $self->{baseOID} .'.' .$alarmID .'.' .$ref->{alarmID},
      asn => ASN_INTEGER,
      value => $ref->{alarmID}
      }));

    my $str = $ref->{alarmString};
    my $h = $ref->{hostID};
    $str =~ s/\{HOSTNAME\}/$h/g;
    $self->set(OpenSistemas::OID::Item->new({
      oid => $self->{baseOID} .'.' .$alarmString .'.' .$ref->{alarmID},
      asn => ASN_OCTET_STR,
      value => $str
      }));

    $self->set(OpenSistemas::OID::Item->new({
      oid => $self->{baseOID} .'.' .$alarmSeverity .'.' .$ref->{alarmID},
      asn => ASN_INTEGER,
      value => $ref->{alarmSeverity}
      }));

    $self->set(OpenSistemas::OID::Item->new({
      oid => $self->{baseOID} .'.' .$alarmStatus .'.' .$ref->{alarmID},
      asn => ASN_INTEGER,
      value => $ref->{alarmStatus}
      }));

    $self->set(OpenSistemas::OID::Item->new({
      oid => $self->{baseOID} .'.' .$alarmAck .'.' .$ref->{alarmID},
      asn => ASN_INTEGER,
      value => $ref->{alarmAck},
      eventid => $ref->{alarmID}
      }));

    $self->writable($self->{baseOID} .'.' .$alarmAck .'.' .$ref->{alarmID}, $self);

    $self->set(OpenSistemas::OID::Item->new({
      oid => $self->{baseOID} .'.' .$alarmSeverity .'.' .$ref->{alarmID},
      asn => ASN_INTEGER,
      value => $ref->{alarmSeverity}
      }));

    $self->set(OpenSistemas::OID::Item->new({
      oid => $self->{baseOID} .'.' .$alarmLastUp .'.' .$ref->{alarmID},
      asn => ASN_OCTET_STR,
      value => $ref->{alarmLastUp} ? DateTime->from_epoch(epoch => $ref->{alarmLastUp})->datetime : ''
      }));

    $self->set(OpenSistemas::OID::Item->new({
      oid => $self->{baseOID} .'.' .$hostID .'.' .$ref->{alarmID},
      asn => ASN_OCTET_STR,
      value => $ref->{hostID}
      }));
  }
  $sth->finish();

  return 1;
}

1;
